﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace WebAPI.common
{
    public class ImageUtil
    {
        /// <summary>
        /// 将图片的编码字符串转成图片
        /// </summary>
        /// <param name="imgStr"></param>
        /// <param name="imgFilePath"></param>
        /// <returns></returns>
        public static bool stringToImage(String imgStr, String imgFilePath)
        {
            bool result = false;
            if (imgStr == null)
                return false;

            try
            {
                // Base64解码  
                byte[] bytes = Convert.FromBase64String(imgStr);

                //创建文件，返回一个 FileStream，它提供对 path 中指定的文件的读/写访问。
                using (FileStream stream = File.Create(imgFilePath))
                {
                    //将字节数组写入流
                    stream.Write(bytes, 0, bytes.Length);
                    stream.Close();
                }
                result = true;
            }
            catch (Exception e)
            {
                return false;
            }

            return result;
        }
    }
}